//
//  TXPFragmentDetailPageView.m
//  StayAwhile
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPFragmentDetailPageView.h"

@interface TXPFragmentDetailPageView ()

@property (nonatomic, retain) UIImageView *headImageView;// 头像
@property (nonatomic, retain) UILabel *nameLabel;// 名字
@property (nonatomic, retain) UILabel *timeLabel;// 发布时间
@property (nonatomic, retain) UIImageView *publicImageView; // 发布的图片
@property (nonatomic, retain) UILabel *wordLabel; // 发布的文字;
@property (nonatomic, retain) UIScrollView *scrollView;
@end
@implementation TXPFragmentDetailPageView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT + 69)];
    self.scrollView.contentSize =CGSizeMake(0, 0);
    self.scrollView.pagingEnabled = YES;
    [self addSubview:self.scrollView];
    
    // 头像
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.headImageView.backgroundColor = [UIColor lightGrayColor];
    self.headImageView.layer.cornerRadius = 25;
    self.headImageView.layer.masksToBounds = YES;
    [self.scrollView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.mas_top).offset(20);
        make.left.mas_equalTo (self.mas_left).offset(20);
        make.size.mas_equalTo (CGSizeMake(50, 50));
        
    }];
    // 名字
    self.nameLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:self.nameLabel];
    self.nameLabel.textColor = RGBA(78, 101, 147, 1);
    self.nameLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.mas_top).offset(35);
        make.left.mas_equalTo (self.headImageView.mas_right).offset(10);
        make.size.mas_equalTo (CGSizeMake(200, 20));
        
    }];
    
    // 发布时间
    self.timeLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:self.timeLabel];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    self.timeLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.mas_top).offset(35);
        make.left.mas_equalTo (self.mas_right).offset(- 70);
        make.size.mas_equalTo (CGSizeMake(150, 20));
        
    }];
    
    // 灰色线
    UIView *lineView = [[UIView alloc] init];
    [self.scrollView addSubview:lineView];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.headImageView.mas_bottom).offset(10);
        make.left.mas_equalTo (self.mas_left).offset(20);
        
        make.size.mas_equalTo (CGSizeMake(375 - 40, 1));
        
    }];

    // 发布的文字
    self.wordLabel = [[UILabel alloc] init];
    self.wordLabel.textColor = RGBA(73, 73, 73, 1);
    self.wordLabel.font = [UIFont boldSystemFontOfSize:15];
    self.wordLabel.numberOfLines = 0;
    [self.scrollView addSubview:self.wordLabel];
    [self.wordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (lineView.mas_bottom).offset(20);
        make.left.mas_equalTo (self.mas_left).offset(20);
        make.right.mas_equalTo (self.mas_right).offset(- 20);
        
        
    }];

    // 发布的照片
    self.publicImageView = [[UIImageView alloc] init];
    [self.scrollView addSubview:self.publicImageView];

}

- (void)setFragmentModel:(TXPFragmentModel *)fragmentModel{
    _fragmentModel = fragmentModel;
    NSURL *url = [NSURL URLWithString:_fragmentModel.model.icon];
    [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
    self.nameLabel.text = _fragmentModel.model.uname;
    self.timeLabel.text = _fragmentModel.addtime_f;
    self.wordLabel.text = _fragmentModel.content;
    [self.wordLabel sizeToFit];
    
    
    if (![_fragmentModel.coverimg_wh isEqualToString:@""]) {
        self.publicImageView.contentMode = UIViewContentModeScaleToFill;
        NSURL *imgurl = [NSURL URLWithString:_fragmentModel.coverimg];
        [self.publicImageView sd_setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@""]];
        [self.publicImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo (self.wordLabel.mas_bottom).offset(20);
            make.left.mas_equalTo (self.mas_left).offset(20);
            make.right.mas_equalTo (self.mas_right).offset(- 20);
        }];

        NSArray *wh = [_fragmentModel.coverimg_wh componentsSeparatedByString:@"*"];
//        NSLog(@"wh === %@", wh);
        CGFloat w = [wh[0] floatValue];
        CGFloat h = [wh[1] floatValue];
        
        CGFloat picWidth = WIDTH - 40;
        CGFloat scale = picWidth / w;
        CGFloat picHeight = h * scale;
        NSLog(@"picHeight = %f", picHeight);
        
        self.scrollView.contentSize = CGSizeMake(WIDTH, 150 + self.wordLabel.frame.size.height + picHeight);
        NSLog(@"scrollView  = %f", self.scrollView.contentSize.height);

    } else {
        self.scrollView.contentSize = CGSizeMake(WIDTH, 800 + self.wordLabel.frame.size.height);
    }
    
    
    }
@end
